home *** CD-ROM | disk | FTP | other *** search
/ GameStar 2004 April / Gamestar_61_2004-04_dvdb.iso / DVDStar / Editace / hltp.exe / {app} / Source Code / Zoners Half-Life Tools / makedep.pl < prev    next >
Perl Script  |  2001-04-18  |  5KB  |  195 lines

  1. #!/usr/bin/perl
  2.  
  3. # Copyright (C) 2000  Sean Cavanaugh
  4. # This file is licensed under the terms of the GNU Public License
  5. # (see GPL.txt, or http://www.gnu.org/copyleft/gpl.txt)
  6.  
  7. @filespecs =
  8. (
  9.     '*.cpp',
  10.     '*.c',
  11.     '*.h',  
  12. );
  13.  
  14. @pathspecs =
  15. (
  16.     '',
  17.     'template',
  18.     'common',
  19.     'hlcsg',
  20.     'hlbsp',
  21.     'hlvis',
  22.     'hlrad',
  23.     'netvis',
  24. );
  25.  
  26. $debug = 0;
  27. $bUseIncludeEnv = 0;
  28. $MODE = "\$(OUTDIR)";
  29. #$EXT = ".o";
  30. #$EXT = ".obj";
  31.  
  32.  
  33. # rip include references out of source files
  34. sub ScanSource
  35. {
  36.     my ($file) = @_;
  37.     
  38.     my @rval = ();
  39.     open(SOURCEFILE, "<$file") || die "Could not open file $file\n";
  40.     while (<SOURCEFILE>)
  41.     {
  42.         if (/^[\s]*#include[\s]*/)
  43.         {
  44.             s/^[\s]*#include[\s]*//;    # remove #include and trailing characters
  45.             s/[\<\>\"\n]//g;            # remove all quotes and <> around filename
  46.             s/\.h.*/\.h/i;              # truncate everything after the .h
  47.             push(@rval, $_);            # add the include reference to the list
  48.         }
  49.     }
  50.     close SOURCEFILE;   
  51.     return @rval;
  52. }
  53.  
  54. my $table;
  55. my $flagged;
  56.  
  57. # attempt to locate header in include path/pathspecs, remove from list if not found
  58. sub TranslateHeaders
  59. {
  60.     my ($file, @headers) = @_;
  61.     
  62.     my @translated = ();
  63.     
  64.     my @searchpaths = @pathspecs;
  65.     push @searchpaths, '';
  66.                 
  67.     foreach my $header (@headers)
  68.     {
  69.         my $found = 0;
  70.         foreach my $path (@searchpaths)
  71.         {
  72.             if (!$found)
  73.             {
  74.                 my $prefix;
  75.                 if ($path eq "")
  76.                 {
  77.                     $prefix = '';
  78.                 }
  79.                 else
  80.                 {
  81.                     $prefix = "$path/";
  82.                 }
  83.                 my $find = $prefix.$header;
  84.                 print STDERR "searching for [$find]\n" if $debug;
  85.                 if (-e $find)
  86.                 {
  87.                     $_ = $find;
  88.                     my $translatedfile = $_;
  89.                     push @translated, $translatedfile;
  90.                     print STDERR "found $translatedfile\n" if $debug;
  91.                     $found = 1;
  92.                 }
  93.             }
  94.         }
  95.         if (!$found)
  96.         {
  97.             print STDERR "removing unfindable header $header\n";
  98.         }
  99.     }
  100.  
  101.     print STDERR "file [$file] depends on @translated\n" if $debug;
  102.     $table{$file}= [ @translated ];
  103.     $flagged{$file}=0;
  104.  
  105. # TODO: handle environemnt variable INCLUDE
  106.     
  107.     return @translated;
  108. }
  109.  
  110. sub RecurseGenerateList
  111. {
  112.     my ($filename) = @_;
  113.     
  114.     my $rval = $filename;
  115.     $flagged{$filename} = 1; # prevent dependency recursion
  116.  
  117.     print STDERR "begin [$filename]:[@{$table{$filename}}]\n" if $debug;
  118.     
  119.     foreach $item ( @{$table{$filename}} )
  120.     {
  121.         if ($flagged{$item} == 1)
  122.         {
  123.             print STDERR "$filename flagged already\n" if $debug;
  124.             goto skip;
  125.         }
  126.         if ($item eq $filename)
  127.         {
  128.             print STDERR "item == filename ($filename)\n" if $debug;
  129.             goto skip;
  130.         }
  131.         if ($item eq "")
  132.         {
  133.             print STDERR "item == null\n" if $debug;
  134.             goto skip;
  135.         }
  136.         $rval .= ' '.RecurseGenerateList($item);
  137.         $flagged{$item} = 1; # prevent include recursion
  138. skip:
  139.     }
  140.     
  141.     return $rval;
  142. }
  143.  
  144. sub ResetFlags
  145. {
  146.     foreach $item ( keys %flagged )
  147.     {
  148.         $flagged{$item} = 0;
  149.     }
  150. }
  151.  
  152.         
  153.  
  154. sub main
  155. {
  156.     foreach my $path (@pathspecs)
  157.     {
  158.         foreach my $filespec (@filespecs)
  159.         {
  160.             my $search;
  161.             if ($path eq "")
  162.             {
  163.                 $search = $filespec;
  164.             }
  165.             else
  166.             {
  167.                 $search = "$path/$filespec";
  168.             }
  169.             my @files = glob($search);
  170.  
  171.             foreach my $file ( @files )
  172.             {
  173.                 TranslateHeaders($file, ScanSource($file));
  174.             }
  175.         }
  176.     }            
  177.     
  178.     my $x = 0;
  179.     foreach $item ( sort keys %flagged )
  180.     {
  181.         print STDERR "working on file [$item]\n" if $debug;
  182.         ResetFlags();
  183.         my $biglist = RecurseGenerateList($item);
  184.         $_ = $item;
  185.         if (s/([\w]*)\.(cpp|c)/$MODE\/$1\$(OBJEXT)/gi)    # We only care about object files and their dependencies
  186.         {
  187.             print STDOUT "$_: $biglist\n";
  188.         }
  189.     }
  190.     $x++;
  191.     return 0;
  192. }
  193.  
  194. &main;
  195.